package org.example.service.impl;

import org.example.client.CommityClient;
import org.example.pojo.CommunityContent;
import org.example.mapper.CommunityContentMapper;
import org.example.pojo.Users;
import org.example.service.ICommunityContentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhx
 * @since 2025-01-17
 */
@Service
public class CommunityContentServiceImpl extends ServiceImpl<CommunityContentMapper, CommunityContent> implements ICommunityContentService {
@Autowired
private CommunityContentMapper communityContentMapper;

@Autowired
private CommityClient commityClient;
    @Override
    public List<CommunityContent> getAllContent(CommunityContent content) {
        List<Users> users=commityClient.getAllUsers();
        HashMap<Integer, String> map = new HashMap<>();
        HashMap<Integer, String> map1 = new HashMap<>();
        for (Users user : users) {
            map.put(user.getUserId(),user.getNickname());
            map1.put(user.getUserId(),user.getAvatar());
        }
        List<CommunityContent> communityContents = communityContentMapper.getAllContent(content);
        for (CommunityContent communityContent : communityContents) {
            communityContent.setNickname(map.get(communityContent.getUserId()));
            communityContent.setAvatar(map1.get(communityContent.getUserId()));
        }
        return communityContents;
    }
    @Override
    public List<CommunityContent> getContentByUserId(Integer userId) {
        List<Users> users=commityClient.getAllUsers();
        Map<Integer, Users> map = new HashMap<>();
        for (Users user : users) {
            map.put(user.getUserId(), user);
        }
        List<CommunityContent> communityContent=communityContentMapper.getContentByUserId(userId);
        for (CommunityContent content : communityContent) {
            content.setNickname(map.get(content.getUserId()).getNickname());
            content.setAvatar(map.get(content.getUserId()).getAvatar());
            content.setSignature(map.get(content.getUserId()).getSignature());
            content.setFollowCount(map.get(content.getUserId()).getFollowCount());
            content.setFollowerCount(map.get(content.getUserId()).getFollowerCount());
            content.setLikeReceivedCount(map.get(content.getUserId()).getLikeReceivedCount());
        }
        return communityContent;
    }

    @Override
    public void likeContent(Integer contentId) {
        communityContentMapper.likeContent(contentId);
    }

    @Override
    public void unlikeContent(Integer contentId) {
        communityContentMapper.unlikeContent(contentId);
    }

    @Override
    public void shareContent(Integer contentId) {
        communityContentMapper.shareContent(contentId);
    }

    @Override
    public void unshareContent(Integer contentId) {
        communityContentMapper.unshareContent(contentId);
    }


}
